package com.tengju.bff.interfaces.covercharge.controller.portal;

import com.idanchuang.component.base.JsonResult;
import com.idanchuang.component.redis.util.DRedisLocks;
import com.tengju.bff.interfaces.covercharge.constant.BizConstants;
import com.tengju.bff.interfaces.covercharge.constant.CoverChargeErrorCode;
import com.tengju.bff.interfaces.covercharge.entity.vo.InvoiceVo;
import com.tengju.bff.interfaces.covercharge.service.IServiceInvoiceShippingLogService;
import com.tengju.bff.interfaces.covercharge.util.UserUtils;
import com.tengju.bff.interfaces.covercharge.util.validator.ValidaUtil;
import com.tengju.bff.interfaces.shared.ApiResponse;
import com.zoi7.component.core.exception.ServiceException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 自认证服务费提现发票快递信息表 前端控制器
 * </p>
 *
 * @author fwy
 * @since 2020-03-09
 */
@RestController
@Slf4j
@RequestMapping("/api/invoiceShippingLog")
@Api(value = "invoiceShippingLog", tags = "提现发票快递信息提交")
public class ServiceInvoiceShippingLogController {

    @Autowired
    private IServiceInvoiceShippingLogService serviceInvoiceShippingLogService;

    @PostMapping("/apply")
    @ResponseBody
    @ApiOperation(value = "提交邮寄发票")
    public ApiResponse<?> apply(HttpServletRequest request, @RequestBody InvoiceVo invoiceVo) {

        //基础参数校验
        invoiceVo.setFrom(UserUtils.getPlatformCode(request));
        ValidaUtil.baseReqParamCheck(invoiceVo);

        return DRedisLocks.runWithLock(BizConstants.INVOICE_APPLY + invoiceVo.getWithdrawId(), 500, () -> {
            try {
                serviceInvoiceShippingLogService.apply(invoiceVo);
                return ApiResponse.newSuccess();
            }catch (ServiceException e){
                log.error("invoiceApply error",e);
                return ApiResponse.newFailure(CoverChargeErrorCode.BUSINESS_ERROR.getCode(),e.getMessage());
            }catch (Exception e){
                log.error("invoiceApply error",e);
                return ApiResponse.newFailure(CoverChargeErrorCode.SYS_ERROR.getCode(),CoverChargeErrorCode.SYS_ERROR.getMsg());
            }
        });

    }

}
